﻿package com.bn.NNYY;

public class FishGeneratorThread extends Thread
{
	GameView gameview;
	private boolean fishgeneratorflag=false;//标志位
	
	public FishGeneratorThread(GameView gameview)
	{
		this.gameview=gameview;
	}
	 
	public void run()
	{
		while(isFishgeneratorflag())
		{
			try
			{
				synchronized (gameview.afish) {
					//生成鱼
					float probability=(float) Math.random();//出现三种鱼的概率
					if(probability<0.85)//出小鱼的几率大
					{
						SingleFish smallfish=new SingleFish(gameview.fish3,Constant.SMALL_FISH_SPEED,1);
						gameview.afish.add(smallfish);//将鱼放入ArrayList中
					}
					else if(probability>=0.85&&probability<=0.95)//其次出中间大的鱼
					{
						SingleFish midfish=new SingleFish(gameview.fish2,Constant.MID_FISH_SPEED,2);
						gameview.afish.add(midfish);//将鱼放入ArrayList中
					}
					else //出大鱼的几率最小
					{
						SingleFish bigfish=new SingleFish(gameview.fish1,Constant.BIG_FISH_SPEED,3);
						gameview.afish.add(bigfish);//将鱼放入ArrayList中
					}
					
					//删除指定区域的鱼
					for(SingleFish delfish:gameview.afish)
					{
						if(delfish.x>Constant.SCREEN_WIDTH+100||delfish.y>Constant.SCREEN_HEIGHT+100||
								delfish.x<-100||delfish.y<-100)//删除游出屏幕的鱼
						{
							gameview.del.add(delfish);
						}
					}
					gameview.judgeInFishingNet();//判断鱼是否在渔网内，在则先在渔网上面生成一条鱼，再删除游动的鱼，
					for(SingleFish fish:gameview.del)//删除指定区域的鱼
					{
						gameview.afish.remove(fish);
					}
				}
				Thread.sleep(250);//每隔0.5秒钟生成一条新的鱼				
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public void setFishgeneratorflag(boolean fishgeneratorflag) {
		this.fishgeneratorflag = fishgeneratorflag;
	}

	public boolean isFishgeneratorflag() {
		return fishgeneratorflag;
	}
}
